import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health/health.controller';
import { MiniProgramController } from './miniprogram/miniprogram.controller';
import { MiniProgramService } from './miniprogram/miniprogram.service';
import { MiniProgramConfig } from './miniprogram/entities/miniprogram-config.entity';
import { MiniProgramSession } from './miniprogram/entities/miniprogram-session.entity';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.local', '.env'],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('DB_HOST', 'localhost'),
        port: configService.get('DB_PORT', 3306),
        username: configService.get('DB_USERNAME', 'root'),
        password: configService.get('DB_PASSWORD', 'password'),
        database: configService.get('DB_DATABASE', 'microservices_miniprogram'),
        entities: [MiniProgramConfig, MiniProgramSession],
        synchronize: configService.get('NODE_ENV') !== 'production',
        logging: configService.get('NODE_ENV') !== 'production',
      }),
      inject: [ConfigService],
    }),
    TypeOrmModule.forFeature([MiniProgramConfig, MiniProgramSession]),
    TerminusModule,
  ],
  controllers: [HealthController, MiniProgramController],
  providers: [MiniProgramService],
  exports: [MiniProgramService],
})
export class AppModule { }
